<template>
  <div class="login-container">
    <el-form class="login-form" autoComplete="on" :model="loginForm" :rules="loginRules" ref="loginForm" label-position="left">
      <h3 class="title">找回密码</h3>
      <el-form-item prop="home">
        <span class="svg-container svg-container_login">
          <svg-icon icon-class="电话" />
        </span>
        <el-input name="home" type="text" v-model="loginForm.account" autoComplete="on" placeholder="电话号码" />
      </el-form-item>
      <el-form-item prop="code" :inline="true" style="display: flex;flex-direction: column">
        <el-col :span="3" class="svg-container svg-container_login">
          <svg-icon icon-class="email" />
        </el-col>
        <el-col :span="15"><el-input class="i" name="account" type="text" v-model="loginForm.tu_code" autoComplete="on" placeholder="验证码" /></el-col>
        <el-col :span="6"> <input type="button" class="btn" :disabled="countFlag" @click="btn" v-model="btnMsg == null ? countNum+'s后重新发送' : btnMsg"></el-col>
      </el-form-item>
      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.password" autoComplete="on"
                  placeholder="新密码"></el-input>
        <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
      </el-form-item>

      <el-form-item prop="repassword">
        <span class="svg-container">
          <svg-icon icon-class="password"></svg-icon>
        </span>
        <el-input name="password2" :type="pwdType" @keyup.enter.native="handleLogin" v-model="loginForm.repassword" autoComplete="on"
                  placeholder="确认新密码"></el-input>
        <span class="show-pwd" @click="showPwd"><svg-icon icon-class="eye" /></span>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" style="width:100%; background: #ff601e" :loading="loading" @click.native.prevent="handleLogin">
          提交
        </el-button>
      </el-form-item>
      <div class="tips">
        <span style="margin-right:20px;"><router-link to="/login">>>>去登陆</router-link></span>
      </div>
    </el-form>
  </div>
</template>
<script>
  import { Message } from 'element-ui'
  import { find_pass ,btn_code } from '@/api/login'
export default {
    name: 'register',
    data() {
      const validatehome = (rule, value, callback) => {
        const regEmail = /^[A-Za-z0-9\u4e00-\u9fa5]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/
        const reg = /^1[345789]\d{9}$/
        if (!reg.test(value) || regEmail.test(value) || value !== '') {
          callback()
        } else {
          callback(new Error('电话或邮箱不正确'))
        }
      }
      const validatePass = (rule, value, callback) => {
        if (value.length < 5) {
          callback(new Error('密码不能小于5位'))
        } else {
          callback()
        }
      }
      const validatePass2 = (rule, value, callback) => {
        if (this.loginForm.repassword !== this.loginForm.password) {
          callback(new Error('两次输入密码不一致!'))
        } else {
          callback()
        }
      }
      return {
        loginForm: {
          account: '18716798410',
          password: '',
          repassword: '',
          tu_code: ''
        },
        countFlag: false,
        countNum: 6,
        btnMsg: '点击发送验证码',
        intervalBtn: {},
        loginRules: {
          home: [{ required: true, trigger: 'blur', validator: validatehome }],
          password: [{ required: true, trigger: 'blur', validator: validatePass }],
          repassword: [{ required: true, trigger: 'blur', validator: validatePass2 }]
        },
        loading: false,
        pwdType: 'password'
      }
    },
    methods: {
      showPwd() {
        if (this.pwdType === 'password') {
          this.pwdType = ''
        } else {
          this.pwdType = 'password'
        }
      },
      handleLogin() {
        this.$refs.loginForm.validate(valid => {
          if (valid) {
            console.log(this.loginForm)
            find_pass( this.loginForm)
              .then(function(response) {
                console.log(response)
              }).catch(error => {
                console.log(error)
            })
          }
        })
      },
      btn() {
        this.btnMsg = null
        // 更改btn状态
        this.countFlag = !this.countFlag
        // 设置倒计时
        this.intervalBtn = setInterval(() => {
          if (this.countNum <= 0) {
            // 重置btn提示信息
            this.btnMsg = '点击发送验证码'
            // 清除定时器
            clearInterval(this.intervalBtn)
            // 更改btn状态
            this.countFlag = !this.countFlag

            // 重置倒计时状态
            this.countNum = 5
          }
          // 倒计时
          this.countNum--
        }, 1000)
        btn_code( { account: this.loginForm.account })
          .then(function(response) {
            console.log(response)
          }).catch(error => {
            console.log(error)
        })
      }
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss">
  $bg: #ffffff;
  $light_gray: #212121;

  /* reset element-ui css */
  .login-container {
    .el-input {
      display: inline-block;
      height: 47px;
      width: 85%;
      input {
        border: 0px;
        -webkit-appearance: none;
        border-radius: 0px;
        padding: 12px 5px 12px 15px;
        color: $light_gray;
        height: 20px;
        &:-webkit-autofill {
          -webkit-box-shadow: 0 0 0px 1000px $bg inset !important;
          -webkit-text-fill-color: #fff !important;
        }
      }
    }
    .el-form-item {
      border: 1px solid #e5e5e5;
      background: rgba(255, 255, 255, 0.1);
      border-radius: 5px;
      color: #e5e5e5;
    }
  }

</style>

<style rel="stylesheet/scss" lang="scss" scoped>
  $dark_gray: #212121;
  $light_gray: #ff601e;
  .login-container {
    position: fixed;
    height: 100%;
    width: 100%;
    background: linear-gradient(#ff362e, #ff601e, #ff9b35);
    .login-form {
      background-color:#ffffff;
      position: absolute;
      left: 0;
      right: 0;
      width: 520px;
      padding: 35px 35px 15px 35px;
      margin: 120px auto;
    }
    .tips {
      font-size: 14px;
      color: $light_gray;
      margin-bottom: 10px;
      span {
        &:first-of-type {
          margin-right: 16px;
        }
      }
    }
    .svg-container {
      padding: 6px 5px 6px 15px;
      color: $dark_gray;
      vertical-align: middle;
      width: 30px;
      display: inline-block;
      &_login {
        font-size: 20px;
      }
    }
    .title {
      font-size: 26px;
      font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .show-pwd {
      position: absolute;
      right: 10px;
      top: 7px;
      font-size: 16px;
      color: $dark_gray;
      cursor: pointer;
      user-select: none;
    }
    .btn{
      margin-top: 8px;
      margin-left: -14px;
      text-align: center;
      width: 144px;
      height: 36px;
      line-height: 36px;
      border-radius: 3px;
      background: #ff601e;
      color: white;
      border: solid 0 red;
    }
    .i{
     height: 52px;
      line-height: 52px;margin-left: 5px;
    }
  }
</style>
